package com.weiaibenpao.demo.chislim.ui.fragment;

import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.weiaibenpao.demo.chislim.R;
import com.weiaibenpao.demo.chislim.base.BaseFragment;
import com.weiaibenpao.demo.chislim.bean.ImageInfo;
import com.weiaibenpao.demo.chislim.bean.MeBean;
import com.weiaibenpao.demo.chislim.bean.UserBean;
import com.weiaibenpao.demo.chislim.db.preferences.UserPreferences;
import com.weiaibenpao.demo.chislim.presenter.MeFragmentPresenter;
import com.weiaibenpao.demo.chislim.ui.activity.me.GradeActivity;
import com.weiaibenpao.demo.chislim.ui.activity.me.MedalActivity;
import com.weiaibenpao.demo.chislim.ui.activity.me.MyMarathonActivity;
import com.weiaibenpao.demo.chislim.ui.activity.me.SetActivity;
import com.weiaibenpao.demo.chislim.ui.activity.me.SportHistoryActivity;
import com.weiaibenpao.demo.chislim.ui.activity.me.UserInfoActivity;
import com.weiaibenpao.demo.chislim.ui.activity.me.UserZoneActivity;
import com.weiaibenpao.demo.chislim.ui.widget.CircleImage;
import com.weiaibenpao.demo.chislim.utils.Utils;
import com.weiaibenpao.demo.chislim.view.MeFragementView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.OnClick;

import static com.weiaibenpao.demo.chislim.db.preferences.UserPreferences.USER_AVATAR;
import static com.weiaibenpao.demo.chislim.db.preferences.UserPreferences.USER_NAME;

/**
 * Created by Administrator on 2017/12/4.
 */

public class MeFragment extends BaseFragment<MeFragementView, MeFragmentPresenter> implements MeFragementView {

    @BindView(R.id.userImg)
    CircleImage userImg;         //用户头像
    @BindView(R.id.userName)
    TextView userName;         //用户信息
    @BindView(R.id.userInfo)
    TextView userInfo;         //用户粉丝，动态，关注
    @BindView(R.id.userInfoLayout)
    RelativeLayout userInfoLayout;     //用户信息模块
    @BindView(R.id.gradeName)
    TextView gradeName;          //用户等级称号
    @BindView(R.id.gradeNameLayout)
    RelativeLayout gradeNameLayout;     //用户等级模块
    @BindView(R.id.gradeNow)
    TextView gradeNow;         //用户当前等级
    @BindView(R.id.gradeNext)
    TextView gradeNext;        //用户下一个等级
    @BindView(R.id.nextValue)
    TextView nextValue;           //用户句下一等级还有多少公里
    @BindView(R.id.userSportGradeLayout)
    RelativeLayout userSportGradeLayout;      //用户运动等级模块
    @BindView(R.id.sportDisText)
    TextView sportDisText;                //用户运动总里程
    @BindView(R.id.sportTimeText)
    TextView sportTimeText;             //用户运动总时间
    @BindView(R.id.sportDataLayout)
    RelativeLayout sportDataLayout;         //用户运动数据模块
    @BindView(R.id.myMatchLayout)
    RelativeLayout myMatchLayout;          //用户我的赛事模块
    @BindView(R.id.myMedalLayout)
    RelativeLayout myMedalLayout;         //用户勋章模块
    @BindView(R.id.mySetLayout)
    RelativeLayout mySetLayout;         //设置模块
    @BindView(R.id.seekBar)
    SeekBar seekBar;           //等级进度条
    double pro;    //等级进度条进度
    double disPoor;    //还差的距离
    int grade;          //当前等级

    String nowGrade="lv0";
    String nextGrade="lv1";

    java.text.DecimalFormat   df;

    UserPreferences userPreferences;
    private int userId;
    private MeBean.GradeBean gradeBean;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_me;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        userPreferences = UserPreferences.getInstance();
        userId=userPreferences.getInt(UserPreferences.USER_ID);
        EventBus.getDefault().register(this);

        Typeface typeFace = Typeface.createFromAsset(getContext().getAssets(), "fonts/Reducto Condensed SSi Condensed.ttf");
        gradeNow.setTypeface(typeFace);
        gradeNext.setTypeface(typeFace);
        sportDisText.setTypeface(typeFace);
        sportTimeText.setTypeface(typeFace);

        seekBar.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                return true;
            }
        });

         userName.setText(userPreferences.getString(USER_NAME));
         Glide.with(this)
                .load(userPreferences.getString(USER_AVATAR))
//                 .transform(new GlideCircleTransform(mContext))
                .into(userImg);
        mPresenter.getMeFargmentBean(userId);

    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden){
            mPresenter.getMeFargmentBean(userId);

        }
    }

    @OnClick({ R.id.userSportGradeLayout,
            R.id.sportDataLayout, R.id.myMatchLayout, R.id.myMedalLayout, R.id.mySetLayout, R.id.userInfoLayout,R.id.userImg})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.userImg:
                //前往用户信息
                startActivity(new Intent(getActivity(), UserInfoActivity.class));
                break;
            case R.id.userSportGradeLayout:
                //前往等级
               GradeActivity.startSelf(getContext(),gradeBean,nowGrade,nextGrade, pro,Utils.decimalFormat(disPoor));
//                Intent intent = new Intent(getActivity(),GradeActivity.class);
//                intent.putExtra("pro",pro);
//                intent.putExtra("disPoor",disPoor);
//                intent.putExtra("grade",grade);
//                intent.putExtra("nowGrade",nowGrade);
//                intent.putExtra("nextGrade",nextGrade);
//                startActivity(intent);
                break;
            case R.id.sportDataLayout:
                //前往运动数据
                startActivity(new Intent(getActivity(), SportHistoryActivity.class));
                break;
            case R.id.myMatchLayout:
                //前往我的赛事比赛
                MyMarathonActivity.startSelf(getContext());
                break;
            case R.id.myMedalLayout:
                startActivity(new Intent(getActivity(), MedalActivity.class));
                //前往勋章
                break;
            case R.id.mySetLayout:
                //前往设置
                startActivity(new Intent(getActivity(), SetActivity.class));
                break;
            case R.id.userInfoLayout:
                //前往个人中心
                UserZoneActivity.startSelf(getContext(),userId);
                break;
        }
    }

    @Override
    public void getDataSuccess(MeBean meBean) {
        gradeBean = meBean.getGrade();
        //0 关注 · 0粉丝 · 0动态
        userInfo.setText(meBean.getCountCares() + " 关注 · " + meBean.getCountFans() + " 粉丝 · " + meBean.getHumorCount() + " 动态");
        sportDisText.setText(Utils.decimalFormat(meBean.getSumDistance()));
        sportTimeText.setText(Utils.decimalFormat(meBean.getSumTime()/3600f));
        if (gradeBean!=null)setData(meBean);
    }

    private void setData(MeBean meBean) {
            gradeName.setText("当前等级：" + gradeBean.getGradeName());
            if (gradeBean.getGradeNumber()==0){
                setSeekBar(0,30, meBean.getSumDistance());
                nowGrade = "lv0";
                nextGrade = "lv1";
                gradeNow.setText("lv0");
                gradeNext.setText("lv1");
            }else if (gradeBean.getGradeNumber()==1){
                setSeekBar(30,100, meBean.getSumDistance());
//            grade = 1;
                nowGrade = "lv1";
                nextGrade = "lv2";
                gradeNow.setText("lv1");
                gradeNext.setText("lv2");
            }else if (gradeBean.getGradeNumber()==2){
                setSeekBar(100,300, meBean.getSumDistance());
//            grade = 2;
                nowGrade = "lv2";
                nextGrade = "lv3";
                gradeNow.setText("lv2");
                gradeNext.setText("lv3");
            }else if (gradeBean.getGradeNumber()==3){
                setSeekBar(300,600, meBean.getSumDistance());
//            grade = 3;
                nowGrade = "lv3";
                nextGrade = "lv4";
                gradeNow.setText("lv3");
                gradeNext.setText("lv4");
            }else if (gradeBean.getGradeNumber()==4) {
                setSeekBar(600,1200, meBean.getSumDistance());
//            grade = 4;
                nowGrade = "lv4";
                nextGrade = "lv5";
                gradeNow.setText("lv4");
                gradeNext.setText("lv5");
            }else if (gradeBean.getGradeNumber()==5) {
                setSeekBar(1200,2500, meBean.getSumDistance());
//            grade = 5;
                nowGrade = "lv5";
                nextGrade = "lv6";
                gradeNow.setText("lv5");
                gradeNext.setText("lv6");
            } else if (gradeBean.getGradeNumber()==6) {
                setSeekBar(2500,5000, meBean.getSumDistance());
//            grade = 6;
                nowGrade = "lv6";
                nextGrade = "lv7";
                gradeNow.setText("lv6");
                gradeNext.setText("lv7");
            }else if (gradeBean.getGradeNumber()==7) {
                seekBar.setProgress(100);

//            grade = 6;
                nowGrade = "lv6";
                nextGrade = "lv7";
                gradeNow.setText("lv6");
                gradeNext.setText("lv7");
                nextValue.setText("最强跑者");

            }


    }

    //设置等级进度
    public void setSeekBar(double minDis, double maxDis, double dis) {
        pro = (dis - minDis) / maxDis;
        seekBar.setMax(100);
        seekBar.setProgress((int)(pro*100));
        disPoor = maxDis-dis;
        nextValue.setText("距离升级还差" + Utils.decimalFormat(disPoor) + "公里");
    }


    @Override
    public MeFragmentPresenter initPresenter() {
        return new MeFragmentPresenter();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(ImageInfo imageInfo) {
        Glide.with( getContext()).load(imageInfo.getIsUpData()).into(userImg);
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventName(UserBean userBean) {
        userName.setText(userBean.getUserName());
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);

    }
}
